package cn.rxls.community.service.impl;

import cn.rxls.community.dto.booking.AddBookingDTO;
import cn.rxls.community.dto.booking.PageBookingDTO;
import cn.rxls.community.entity.Booking;
import cn.rxls.community.enums.BookingStateEnum;
import cn.rxls.community.enums.ResultEnum;
import cn.rxls.community.exception.BusinessException;
import cn.rxls.community.mapper.BookingMapper;
import cn.rxls.community.service.BookingService;
import cn.rxls.community.util.HttpUtil;
import cn.rxls.community.vo.booking.BookingVO;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.time.LocalDate;
import java.util.List;
import java.util.Locale;

/**
 * 预约(Booking)表服务实现类
 */
@Service("bookingService")
@Transactional
public class BookingServiceImpl extends ServiceImpl<BookingMapper, Booking> implements BookingService {

    private final BookingMapper bookingMapper;

    BookingServiceImpl(BookingMapper bookingMapper) {
        this.bookingMapper = bookingMapper;
    }

    /**
     * 新增预约
     *
     * @param dto 参数悐
     * @return 是否成功
     */
    @Override
    public Boolean addBooking(AddBookingDTO dto) {
        //获取用户信息
        String uid = String.valueOf(HttpUtil.getUserLoginInfo().get("uid"));
        //查询当天是否还有预约
        int dayOfMonth = LocalDate.now().getDayOfMonth();
        Integer integer = bookingMapper.selectCountByDay(dayOfMonth,Long.valueOf(uid), BookingStateEnum.SUBMIT.code);
        if (integer > 1) {
            throw new BusinessException(ResultEnum.ERROR_BOOKING_REPEAT);
        }
        Booking booking = new Booking();
        booking.setBookingCall(dto.getCall());
        booking.setBookingUser(Long.valueOf(uid));
        booking.setBookingTime(dto.getTimer());
        booking.setBookingPhone(dto.getPhone());
        booking.setBookingAddress(dto.getAddress());
        booking.setBookingState(BookingStateEnum.SUBMIT.code);
        return bookingMapper.insert(booking) > 0;
    }

    /**
     * 根据状态分页查询
     *
     * @param dto 参数对象
     * @return 多条信息
     */
    @Override
    public List<BookingVO> selectPageByState(PageBookingDTO dto) {
        //获取用户信息
        String uid = String.valueOf(HttpUtil.getUserLoginInfo().get("uid"));
        Integer codeByMsg = BookingStateEnum.getCodeByMsg(dto.getState());
        dto.setPage((dto.getPage() - 1) * dto.getSize());
        return bookingMapper.selectPageByState(dto.getPage(), dto.getSize(), codeByMsg,Long.valueOf(uid));
    }

    /**
     * 取消预约
     *
     * @param id 预约编号
     * @return 取消结果
     */
    @Override
    public Boolean cancelBooking(Long id) {
        //获取用户信息
        String uid = String.valueOf(HttpUtil.getUserLoginInfo().get("uid"));
        LambdaQueryWrapper<Booking> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(Booking::getBookingId, id).eq(Booking::getBookingUser,Long.valueOf(uid));
        Booking booking = new Booking();
        booking.setBookingState(BookingStateEnum.CANCEL.code);
        return bookingMapper.update(booking, wrapper) > 0;
    }
}

